package cn.iocoder.yudao.module.erp.controller.admin.outinrule;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.outinrule.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.outinrule.OutInRuleDO;
import cn.iocoder.yudao.module.erp.service.outinrule.OutInRuleService;

@Tag(name = "管理后台 - ERP 出入库规则")
@RestController
@RequestMapping("/erp/out-in-rule")
@Validated
public class OutInRuleController {

    @Resource
    private OutInRuleService outInRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 出入库规则")
    @PreAuthorize("@ss.hasPermission('erp:out-in-rule:create')")
    public CommonResult<Long> createOutInRule(@Valid @RequestBody OutInRuleSaveReqVO createReqVO) {
        return success(outInRuleService.createOutInRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 出入库规则")
    @PreAuthorize("@ss.hasPermission('erp:out-in-rule:update')")
    public CommonResult<Boolean> updateOutInRule(@Valid @RequestBody OutInRuleSaveReqVO updateReqVO) {
        outInRuleService.updateOutInRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 出入库规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:out-in-rule:delete')")
    public CommonResult<Boolean> deleteOutInRule(@RequestParam("id") Long id) {
        outInRuleService.deleteOutInRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 出入库规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:out-in-rule:query')")
    public CommonResult<OutInRuleRespVO> getOutInRule(@RequestParam("id") Long id) {
        OutInRuleDO outInRule = outInRuleService.getOutInRule(id);
        return success(BeanUtils.toBean(outInRule, OutInRuleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 出入库规则分页")
    @PreAuthorize("@ss.hasPermission('erp:out-in-rule:query')")
    public CommonResult<PageResult<OutInRuleRespVO>> getOutInRulePage(@Valid OutInRulePageReqVO pageReqVO) {
        PageResult<OutInRuleDO> pageResult = outInRuleService.getOutInRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OutInRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 出入库规则 Excel")
    @PreAuthorize("@ss.hasPermission('erp:out-in-rule:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOutInRuleExcel(@Valid OutInRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OutInRuleDO> list = outInRuleService.getOutInRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 出入库规则.xls", "数据", OutInRuleRespVO.class,
                        BeanUtils.toBean(list, OutInRuleRespVO.class));
    }

}